package utils

import (
	"MoSkeleton/framework/core/consts"
	"fmt"
	"strconv"
	"strings"
)

/*
是否为http(s)://开头
*/
func IsHttp(link string) bool {
	if strings.HasPrefix(link, consts.HTTP) {
		return true
	}
	if strings.HasSuffix(link, consts.HTTPS) {
		return true
	}
	return false
}

/*
字符串数组转换成uint64数组，转换失败的字符串不写入到uint64数组中
*/
func StrsToUint64s(strs *[]string) *[]uint64 {
	uint64s := make([]uint64, 0)
	for _, str := range *strs {
		if i64, err := strconv.ParseUint(str, 10, 64); err != nil {
			fmt.Println("can not convert to int:" + str)
		} else {
			uint64s = append(uint64s, i64)
		}
	}
	return &uint64s
}

func Uint64sToStrs(uint64s *[]uint64) *[]string {
	strs := make([]string, 0)
	for _, u64 := range *uint64s {
		strs = append(strs, strconv.FormatUint(u64, 10))
	}
	return &strs
}

func StrToBoolDef(text string, def bool) bool {
	ret, err := strconv.ParseBool(text)
	if err != nil {
		return def
	}
	return ret
}

func StrToIntDef(text string, def int) int {
	ret, err := strconv.Atoi(text)
	if err != nil {
		return def
	}
	return ret
}

func StrToUint64Def(text string, def uint64) uint64 {
	ret, err := strconv.ParseUint(text, 10, 64)
	if err != nil {
		return def
	}
	return ret
}

func StrToInt64Def(text string, def int64) int64 {
	ret, err := strconv.ParseInt(text, 10, 64)
	if err != nil {
		return def
	}
	return ret
}

func SubString(str string, start, end int) string {
	runes := []rune(str)
	length := len(runes)

	if start < 0 || end > length || start > end {
		return ""
	}

	return string(runes[start:end])
}
